package tpenchere.ui.contents;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.RowFilter;

import org.jdesktop.application.ResourceMap;
import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.JXTable;

import tpenchere.application.MainApplication;
import tpenchere.controllers.ControllerInterface;
import tpenchere.controllers.HomeController;
import tpenchere.controllers.LoginController;
import tpenchere.ui.AbstractView;
import tpenchere.ui.model.ProductRowFilter;
import tpenchere.ui.model.ProductTableModel;

import java.awt.Dimension;
import java.util.List;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import java.awt.Component;

public class HomeWindow extends AbstractView {
	private final ResourceMap resourceMap = MainApplication.getApplication().getContext().getResourceMap(this.getClass());
	private HomeController controller;
	private JXTable productList;
	private ProductTableModel productsModel = new ProductTableModel();
	private JTextField searchField;
	private ProductRowFilter rowFilter;

	public HomeWindow() {
		controller = new HomeController(this);
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		productList = new JXTable(productsModel);
		productList.setFillsViewportHeight(true);
		productList.setPreferredScrollableViewportSize(productList.getPreferredSize());
		
		JScrollPane scrollPane = new JScrollPane(productList);
		scrollPane.setPreferredSize(new Dimension(25, 20));
		panel.add(scrollPane, BorderLayout.CENTER);
		
		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.NORTH);
		
		JButton btnNewButton = new JButton(controller.getActionMap().get("view.home.toolbar.new"));
		toolBar.add(btnNewButton);
		
		JButton btnParticipate = new JButton(controller.getActionMap().get("view.home.toolbar.participate"));
		toolBar.add(btnParticipate);
		
		JButton btnDetails = new JButton(controller.getActionMap().get("view.home.toolbar.details"));
		toolBar.add(btnDetails);
		
		toolBar.add(Box.createHorizontalGlue());
		
		JLabel lblCategory = new JLabel("Category");
		toolBar.add(lblCategory);
		searchField = new JTextField("");
		searchField.setPreferredSize(new Dimension(300, 20));
		searchField.setMinimumSize(new Dimension(300, 20));
		searchField.setMaximumSize(new Dimension(300, 20));
		searchField.setAlignmentX(Component.RIGHT_ALIGNMENT);
		rowFilter = new ProductRowFilter(searchField);
		productList.setRowFilter(rowFilter);
		searchField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				productList.setRowFilter(rowFilter);
			}
		});

		toolBar.add(searchField);
		searchField.setColumns(5);
		
		controller.loadProductList();
	}

	public ResourceMap getResourceMap() {
		return resourceMap;
	}

	@Override
	public ControllerInterface getController() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JMenuBar getMenuBar() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<JToolBar> getToolbars() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public JTable getProductList() {
		return productList;
	}

	public void setProductList(JXTable productList) {
		this.productList = productList;
	}

	public ProductTableModel getProductsModel() {
		return productsModel;
	}

	public void setProductsModel(ProductTableModel productsModel) {
		this.productsModel = productsModel;
	}
	
	public void updateView() {
		controller.loadProductList();
	}

}
